<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Topics tagged with micro-ros 话题通信]]></title><description><![CDATA[A list of topics that have been tagged with micro-ros 话题通信]]></description><link>https://fishros.org.cn/forum/tags/micro-ros 话题通信</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 02:12:21 GMT</lastBuildDate><atom:link href="https://fishros.org.cn/forum/tags/micro-ros 话题通信.rss" rel="self" type="application/rss+xml"/><pubDate>Invalid Date</pubDate><ttl>60</ttl><item><title><![CDATA[使用Micro-ROS报错]]></title><description><![CDATA[<p dir="auto">问题解决了，源头出在一个补丁函数：</p>
void * microros_reallocate(void * pointer, size_t size, void * state){
  return NULL; // 静态策略下通常不使用 realloc
}

<p dir="auto">简单来说，就是micro-ROS 试图申请或调整内存时，需要用到这个函数，而我的函数直接返回了NULL值，导致了micro-ROS 无法调整内存<br />
为此，求助LLM，对函数做出以下修改:</p>
void * microros_reallocate(void * pointer, size_t size, void * state){
  // 情况 1: 如果指针为空，这就相当于 malloc (这是 Micro-ROS 初始化最常用的情况)
  if (pointer == NULL) {
    return pvPortMalloc(size);
  }

  // 情况 2: 如果大小为 0，这就相当于 free
  if (size == 0) {
    vPortFree(pointer);
    return NULL;
  }

  // 情况 3: 真正的 realloc (调整大小)
  // 由于 FreeRTOS 的 heap_4 不支持 realloc，我们需要手动“搬家”
  // ⚠️ 警告：我们不知道旧数据块的大小，这是一个风险点。
  // 但 Micro-ROS 通常用它是为了扩大数组。
  // 这里的策略是：申请新内存 -&gt; 假定旧数据有效 -&gt; 释放旧内存
  
  void * new_ptr = pvPortMalloc(size);
  if (new_ptr != NULL) {
      // 这里的拷贝长度是个玄学，因为我们不知道旧块有多大。
      // 但对于 wait_set 这种结构，通常是安全的，或者我们可以只拷贝一部分。
      // 为了安全起见，我们假设旧数据也很重要，拷贝 size 长度 (可能会读越界，但在 RAM 连续的 MCU 上通常没事)
      // 更安全的做法是引入记录内存大小的机制，但太复杂。
      // 这里采用最简单的“能跑就行”策略：
      memcpy(new_ptr, pointer, size); // 注意：如果 size &lt; old_size，会截断；如果 size &gt; old_size，可能会读到垃圾数据
      vPortFree(pointer);
  }
  return new_ptr;
}

<p dir="auto">重新编译烧录，问题解决</p>
]]></description><link>https://fishros.org.cn/forum/topic/4663/使用micro-ros报错</link><guid isPermaLink="true">https://fishros.org.cn/forum/topic/4663/使用micro-ros报错</guid><dc:creator><![CDATA[winstonmanapple]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>